//
//  PersonInfoViewController.m
//  BDD_TWO
//
//  Created by XTW on 2023/5/6.
//

#import "PersonInfoViewController.h"
#import "MBProgressHUD.h"
#import "PersonNameEditViewController.h"

@interface PersonInfoViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIImageView *headerImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;

@end

@implementation PersonInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人信息";
    [self addBackBtn];
    [self.view setBackgroundColor:ViewBackgroundColor];
    [self.contentView setFrame:CGRectMake(0, NavBarHeight + IS_iPhoneX_Top, SCREEN_WIDTH_DEVICE, 169)];
    
    UserInfo *user = [UserManger currentLoggedInUser];
    [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:user.avatar] placeholderImage:[UIImage imageNamed:@"defaultHeader"]];
    self.nameLabel.text = user.userName;
    self.phoneLabel.text = user.mobile;
    // Do any additional setup after loading the view from its nib.
}

- (void)dealloc{
    [[ServiceRequest sharedService] cancelDataTaskForKey:@"image/base64"];
}

- (IBAction)editNamePress {
    PersonNameEditViewController *viewCon = [[PersonNameEditViewController alloc] initWithNibName:@"PersonNameEditViewController" bundle:nil];
    viewCon.nameStr = self.nameLabel.text;
    viewCon.RefreshHandle = ^(NSString * _Nonnull content) {
        self.nameLabel.text = content;
    };
    CustomNavigationCoutroller *navCon = [[CustomNavigationCoutroller alloc] initWithRootViewController:viewCon];
    navCon.modalPresentationStyle = UIModalPresentationFullScreen;
    [self.navigationController presentViewController:navCon animated:YES completion:nil];
}

- (IBAction)headerPress {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"选择图片做为头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *oneBtn = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self changeHeadPortraitCamera];
    }];
    [alertController addAction:oneBtn];
    UIAlertAction *twoBtn = [UIAlertAction actionWithTitle:@"本地相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self changeHeadPortraitFromPicture];
    }];
    [alertController addAction:twoBtn];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark 拍照
- (void)changeHeadPortraitCamera {
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    [self addImagePickerController:sourceType];
}

#pragma mark 从相册中选取
- (void)changeHeadPortraitFromPicture {
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        [self addImagePickerController:sourceType];

}

#pragma mark 选择相册
- (void)addImagePickerController:(UIImagePickerControllerSourceType )sourceType {
    UIImagePickerController * picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = sourceType;
    [self.navigationController.topViewController presentViewController:picker animated:YES completion:nil];
}

#pragma mark 选择相册完毕
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image=[info objectForKey:UIImagePickerControllerEditedImage];
    self.headerImageView.image = image;
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeAnnularDeterminate;
    hud.label.text = @"正在保存";
    [[ServiceRequest sharedService] POSTJSON:@"image/base64" parameters:@{@"base64String":[UIImageJPEGRepresentation(image,0.5)  base64EncodedString]} success:^(id responseObject) {
        NSString *imageUrl = [responseObject objectForKey:@"uploadFileUrl"];
        
        UserInfo *user = [UserManger currentLoggedInUser];
        [[ServiceRequest sharedService] PUTJSON:[NSString stringWithFormat:@"user/%@", user.platformId] parameters:@{@"avatar":imageUrl} success:^(id responseObject) {
            [hud hideAnimated:YES];
            user.avatar = imageUrl;
            [UserManger setUser:user];
        } failure:^(NSString *error, NSInteger code) {
            [hud hideAnimated:YES];
            [self showHUDAlert:error];
        }];
        
    } failure:^(NSString *error, NSInteger code) {
        [hud hideAnimated:YES];
        [self showHUDAlert:error];
    } update:^(float progress) {
        dispatch_async(dispatch_get_main_queue(), ^{
            hud.progress = progress;
        });
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}
@end
